/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wordpress.fareastdan.FEDTradeManagerIDE.FEDConnectionManager.GUI;

import com.wordpress.fareastdan.FEDTradeManagerIDE.FEDConnectionManager.FEDServerRootNode;
import com.wordpress.fareastdan.FEDTradeManagerIDE.FEDConnectionManager.FEDServerChildFactory;
import com.wordpress.fareastdan.FEDTradeManagerIDE.FEDConnectionManager.FEDServer;
import com.wordpress.fareastdan.FEDTradeManagerIDE.FEDConnectionManager.FEDServerNode;
import java.awt.BorderLayout;
import java.util.Collection;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.view.BeanTreeView;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.util.NbBundle.Messages;
import org.openide.util.Utilities;
import org.openide.windows.TopComponent;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//com.wordpress.fareastdan.FEDTradeManagerIDE.FEDConnectionManager.GUI//FEDServerList//EN",
        autostore = false
)
@TopComponent.Description(
        preferredID = "FEDServerListTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_ALWAYS
)
@TopComponent.Registration(mode = "explorer", openAtStartup = true)
@ActionID(category = "Window", id = "com.wordpress.fareastdan.FEDTradeManagerIDE.FEDConnectionManager.GUI.FEDServerListTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_FEDServerListAction",
        preferredID = "FEDServerListTopComponent"
)
@Messages({
    "CTL_FEDServerListAction=Registered Server List",
    "CTL_FEDServerListTopComponent=Servers",
    "HINT_FEDServerListTopComponent=Servers currently managed by this console"
})
public final class FEDServerListTopComponent extends TopComponent implements LookupListener, ExplorerManager.Provider
{

    public FEDServerListTopComponent() 
    {
        initComponents();
        setName(Bundle.CTL_FEDServerListTopComponent());
        setToolTipText(Bundle.HINT_FEDServerListTopComponent());

        setLayout(new BorderLayout());
        add(new BeanTreeView(), BorderLayout.CENTER);
        mgr.setRootContext(new FEDServerRootNode());    
    }
    @Override
    public void componentOpened() 
    {
        // TODO add custom code on component opening
        FEDServerList = Utilities.actionsGlobalContext().lookupResult(FEDServer.class);
        FEDServerList.addLookupListener (this);
    }

    @Override
    public void componentClosed() 
    {
        // TODO add custom code on component closing
        FEDServerList.removeLookupListener(this);
    }

    void writeProperties(java.util.Properties p) 
    {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) 
    {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    public void resultChanged(LookupEvent le)
    {
        // Get the new list of servers
        Collection<? extends FEDServer> allServers = FEDServerList.allInstances();
        
        // If there are servers
        if( false == allServers.isEmpty())
        {
            // Add them to the list
            FEDServer server = allServers.iterator().next();
            addServerToList(server);
        }
        // If there are none, have empty node
        else
        {
            
        }
        
  /*      if (!allEvents.isEmpty()) {
            Event event = allEvents.iterator().next();
            jLabel1.setText(Integer.toString(event.getIndex()));
            jLabel2.setText(event.getDate().toString());
        } else {
            jLabel1.setText("[no selection]");
            jLabel2.setText("");*/
               // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ExplorerManager getExplorerManager()
    {
        return mgr;
    }
    
    private void addServerToList( FEDServer server )
    {
        
    }

    private Lookup.Result<FEDServer> FEDServerList = null;
    private final ExplorerManager mgr = new ExplorerManager();
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
